﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Core.Sponsor {

    public class SponsorTypeAllowedParent : CoreObject {

        #region Private Properties

        private Int64 sponsorTypeId = 0;

        private Int64 parentSponsorTypeId = 0;


        private SponsorType parentSponsorType = null;

        #endregion 


        #region Public Properties

        public Int64 SponsorTypeId { get { return sponsorTypeId; } set { sponsorTypeId = value; } }

        public Int64 ParentSponsorTypeId { get { return parentSponsorTypeId; } set { parentSponsorTypeId     = value; } }

        public SponsorType ParentSponsorType {

            get {

                if (parentSponsorType == null) { parentSponsorType = Application.Core.Sponsor.SponsorType (parentSponsorTypeId, true); }

                return parentSponsorType;

            }

        }

        public String ParentSponsorTypeName { get { return ((ParentSponsorType != null) ? ParentSponsorType.Name : String.Empty); } }

        #endregion 

        
        #region Constructors

        public SponsorTypeAllowedParent (Application forApplication) : base (forApplication) {

            return;

        }

        public SponsorTypeAllowedParent (Application forApplication, Int64 forId) : base (forApplication, forId) {

            return;

        }
        
        #endregion


        #region Database Functions

        protected override Dictionary<String, String> Validate () {

            Dictionary<String, String> validationErrors = new Dictionary<String, String> ();

            // DO NOT CALL THE BASE VALIDATION, NO NEED TO CHECK FOR NAME/DESCRIPTION 

            

            return validationErrors;

        }

        public override void MapDataFields (System.Data.DataRow currentRow) {
            
            base.MapDataFields (currentRow);

            SponsorTypeId = Data.Readers.DataReader.Int64FromDatabase (currentRow, "SponsorTypeId");

            ParentSponsorTypeId = Data.Readers.DataReader.Int64FromDatabase (currentRow, "ParentSponsorTypeId");

            return;

        }

        public override Boolean Save () {

            Boolean success = false;

            try {

                Dictionary<String, String> validationErrors = Validate ();

                if (validationErrors.Count > 0) {

                    Application.SetLastException (new Exception ("Invalid [" + validationErrors.Keys.First () + "]: " + validationErrors.Values.First ()));

                    return false;

                }

                UpdateModifiedToSessionUser ();


                System.Data.IDbCommand command = Application.EnvironmentDatabase.CreateCommand (ObjectType + "_InsertUpdate");

                command.CommandType = System.Data.CommandType.StoredProcedure;


                Application.EnvironmentDatabase.AppendCommandParameter (command, "Id", Id);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "SponsorTypeId", SponsorTypeId);

                Application.EnvironmentDatabase.AppendCommandParameter (command, "ParentSponsorTypeId", (Int64?)((ParentSponsorTypeId != 0) ? ParentSponsorTypeId : (Int64?) null));

                Application.EnvironmentDatabase.AppendCommandParameter (command, "ModifiedId", Application.Session.UserAccountName, 60);

                success = (command.ExecuteNonQuery () > 0);

                if (success) { SetIdentity (); }

            }

            catch (Exception exception) {

                success = false;

                Application.SetLastException (exception);

            }

            return success;

        }

        public override Boolean Delete () {

            return base.DeleteFromDatabase (DeleteStatementById (), Application.EnvironmentDatabase);

        }

        #endregion 


    }

}